
#include "CGame.h"
#include <string>
#include <iostream>
#include <time.h>
using std::string;
#include "source\SGD Wrappers\CSGD_Direct3D.h"
#include "Source\SGD Wrappers\CSGD_TextureManager.h"
#include "Source\SGD Wrappers\CSGD_DirectInput.h"
#include "Source\CMainMenuState.h"
#include "Source\CBitFont.h"
// default constructor
CGame::CGame()
{
	bFullscreen = false;
	bShutdown = false;
}

// destructor
CGame::~CGame() 
{ 

}

// singleton accessor
CGame* CGame::GetInstance()
{
	static CGame instance;
	return &instance;
}

// initialization
void CGame::Initialize(HWND _hWnd, HINSTANCE hInstance, 
	int nScreenWidth, int nScreenHeight, bool bIsWindowed)
{
	
	cout << "Initializing Game\n"; 
	
	_pD3D = CSGD_Direct3D::GetInstance();
	_pTM = CSGD_TextureManager::GetInstance();
	_pDI = CSGD_DirectInput::GetInstance();
	_pD3D->InitDirect3D(_hWnd, nScreenWidth, nScreenHeight, bIsWindowed, false);
	_pTM->InitTextureManager( _pD3D->GetDirect3DDevice(), _pD3D->GetSprite());
	_pDI->InitDirectInput(_hWnd, hInstance, DI_KEYBOARD | DI_MOUSE, DI_MOUSE);

	srand( unsigned int( time( 0 ) ) );
	m_nScreenWidth = nScreenWidth;
	m_nScreenHeight = nScreenHeight;
	int ret = client.Init("127.0.0.1",7500);
	if(ret == SOCKET_ERROR)
		cout << "Socket Error\n";
	else if(ret == ADDRESS_ERROR)
		cout << "Address Error\n";
	else if(ret == CONNECT_TIMEOUT)
		cout << "Connect Time out\n";

	ret = client.Connect(rand()%101/100.0f,rand()%101/100.0f,rand()%101/100.0f);
	if(ret == SOCKET_ERROR)
		cout << "Socket Error\n";
	else if(ret == ADDRESS_ERROR)
		cout << "Address Error\n";
	else if(ret == CONNECT_TIMEOUT)
		cout << "Connect Time out\n";
	_gameState = CMainMenuState::GetInstance();
	_gameState->Enter();
}

// execution
bool CGame::Main()
{
	m_fElapsedTime = 1.0f / 60;
	
	PreUpdate( m_fElapsedTime );
	
	if( !Update( m_fElapsedTime ) )
		return false;

	PostUpdate( m_fElapsedTime );

	return true;
}

void CGame::PreUpdate( float fDT )
{
	_pDI->ReadDevices();
}

bool CGame::Update( float fDT )
{
	if(_pDI->KeyPressed(DIK_F1))
	{
		int ret = client.Connect(rand()%101/100.0f,rand()%101/100.0f,rand()%101/100.0f);
		if(ret == SOCKET_ERROR)
			cout << "Socket Error\n";
		else if(ret == ADDRESS_ERROR)
			cout << "Address Error\n";
		else if(ret == CONNECT_TIMEOUT)
			cout << "Connect Time out\n";
	}
	_gameState->Update(fDT);
	return _gameState->Input();;
}

void CGame::PostUpdate( float fDT )
{
	// Audio Manager Update Here
	//m_MAudioPtr->Update();
	//////////

	// Do Render Processing
	Render();
	//////////

	// File Manager Update Here
	// 
	//////////
	int ret = client.Update(fDT);
}

void CGame::Render()
{
	//////////
	_pD3D->Clear(0, 0, 0);
	_pD3D->DeviceBegin();
	_pD3D->SpriteBegin();

	_gameState->Render();

	_pD3D->SpriteEnd();
	_pD3D->DeviceEnd();
	_pD3D->Present();
}

// cleanup
void CGame::Shutdown()
{
	client.Stop();
	_gameState->Exit();
	if(_pDI)
	{
		_pDI->ShutdownDirectInput();
		_pDI = NULL;
	}
	if(_pTM)
	{
		_pTM->ShutdownTextureManager();
		_pTM = NULL;
	}

	if(_pD3D)
	{
		_pD3D->ShutdownDirect3D();
		_pD3D = NULL;
	}
}